package com.xxx.meter.ui.patrol.machine.info;

import android.content.pm.ActivityInfo;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xxx.meter.R;
import com.xxx.meter.base.BaseActivity;
import com.xxx.meter.model.data.patrol.PatrolUtil;
import com.xxx.meter.model.http.Api;
import com.xxx.meter.model.http.ApiCallback;
import com.xxx.meter.model.http.bean.PatrolBean;
import com.xxx.meter.model.utils.ButtonUtil;
import com.xxx.meter.model.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class PatrolMachineInfoActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.main_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.main_refresh)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.not_data)
    ImageView mNotData;
    @BindView(R.id.main_title)
    TextView mTitle;

    private String machineId;
    private String machineType;
    private PatrolMachineInfoAdapter mAdapter;
    private ArrayList<PatrolBean> mList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_patrol_info;
    }

    @Override
    protected void initData() {
        //禁止横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        machineId = getIntent().getStringExtra("machineId");
        machineType = getIntent().getStringExtra("machineType");
        String machineName = getIntent().getStringExtra("machineName");

        mTitle.setText(machineName);
        mAdapter = new PatrolMachineInfoAdapter(mList);
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecycler.setAdapter(mAdapter);
        mRefresh.setOnRefreshListener(this);

        loadSweepDevice();
    }

    @OnClick({R.id.main_return, R.id.machine_info_submit, R.id.not_data, R.id.not_net})
    public void OnClick(View view) {
        if (ButtonUtil.isFastClick()) return;
        switch (view.getId()) {
            case R.id.main_return:
                finish();
                break;
            case R.id.machine_info_submit:
                finish();
                break;
            case R.id.not_data:
                loadSweepDevice();
                break;
            case R.id.not_net:
                loadSweepDevice();
                break;
        }
    }

    @Override
    public void onRefresh() {
        //刷着玩
        mRefresh.setRefreshing(false);
    }

    /**
     * @Model 加载设备
     */
    private void loadSweepDevice() {
        Api.getInstance().getPatrolDeviceInfoList(machineType, machineId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiCallback<List<PatrolBean>>(this, false) {

                    @Override
                    public void onSuccess(List<PatrolBean> list) {
                        if (list.size() == 0) {
                            mRefresh.setVisibility(View.GONE);
                            mNotData.setVisibility(View.VISIBLE);
                        } else {
                            mRefresh.setVisibility(View.VISIBLE);
                            mNotData.setVisibility(View.GONE);

                            mList.clear();
                            mList.addAll(list);
                            mAdapter.notifyDataSetChanged();
                            mRefresh.setRefreshing(false);
                        }
                        PatrolUtil.updateMachineInfo(machineType, machineId, list);
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        ToastUtil.showToast(errorMessage);
                    }
                });
    }
}
